package zjh_20230403_homework;

import java.io.Serializable;

class Computer implements Cloneable, Serializable {
    public String name;
    public Double price;
    public Cpu cpu;

    /**
     * 浅克隆
     *
     * @return
     * @throws CloneNotSupportedException
     */
//    @Override
//    protected Computer clone() throws CloneNotSupportedException {
//        return (Computer) super.clone();
//    }

    /**
     * 深克隆 重写clone的方法
     * @return
     * @throws CloneNotSupportedException
     */
    @Override
    protected Computer clone() throws CloneNotSupportedException {
        Computer clone = (Computer)super.clone();
        clone.cpu = (Cpu) this.cpu.clone();
        return clone;
    }

    @Override
    public String toString() {
        return super.toString() + "Computer{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", cpu=" + cpu +
                '}';
    }
}

class Cpu implements Cloneable,Serializable {
    public String name;

    /**
     * 浅克隆
     *
     * @return
     * @throws CloneNotSupportedException
     */
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return super.toString() + "Cpu{" +
                "name='" + name + '\'' +
                '}';
    }
}
